import java.util.ArrayList;
import java.util.Comparator;

public class t937 {
    public static void main(String[] args) {
        String arr [] ={"dig1 8 1 5 1","let1 art zero can","dig2 3 6","let2 own kit dig","let3 art zero"};
        String brr[] = reorderLogFiles(arr);
        for(String str :brr){
            System.out.println(str);
        }
    }
    public static String[] reorderLogFiles(String[] logs) {
        ArrayList<String> list_str = new ArrayList<>();
        ArrayList<String> list_nohand = new ArrayList<>();
        ArrayList<String> list_num = new ArrayList<>();
        for(String str : logs){
            String arr[] = str.split(" ");
            char tmp = arr[1].charAt(0);
            if(tmp>='0' && tmp<='9'){
                list_num.add(str);
            }else{
                list_str.add(str);
                str = "";
                for(int i =1;i<arr.length;i++){
                    if(i!=arr.length-1){
                        str+=arr[i]+" ";
                    }else{
                        str+=arr[i];
                    }
                }
                list_nohand.add(str);
              //  System.out.println(str);
            }
        }

        list_str.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        list_nohand.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
               return o1.compareTo(o2);
            }
        });
        System.out.println(list_str);

        System.out.println(list_nohand);
        /*String s = list_nohand.get(1);
        String ss = list_str.get(2);

        System.out.println(s);
        System.out.println(ss);
        System.out.println(ss.indexOf(s));*/

        for(int i =0;i<list_str.size();i++){
            String str = list_nohand.get(i);
            for(int j = 0;j<list_str.size();j++){
                String ss = list_str.get(j);
                //System.out.println(ss);
                //System.out.println(str);
                String arr[] = ss.split(" ");
               // System.out.println(ss.indexOf(str));
                if(ss.endsWith(str)){
                    list_nohand.set(i,arr[0]+" "+str);
                    list_str.set(j,"");
                    break;
                }
            }
            //System.out.println("****");
        }
        list_nohand.addAll(list_num);
        String arr[] = new String[list_nohand.size()];
       for(int i=0;i<list_nohand.size();i++){
           arr[i] = list_nohand.get(i);
       }
       return arr;
        //return null;



    }
    /*public static ArrayList<String> unit (ArrayList<String> list){


    }*/
}

